#include "../main.h"

void addToMap(Map *map, int sockfd, char *msg, int len)
{
    MNode *node = getbykey(map, sockfd);
    if (node != NULL)
    {
        RecvData *recvData = node->record->recvData;
        if (recvData->length > 0 && len > 0)
        {
            int length = recvData->length + len;
            char *buffer = (char *)malloc(length);
            memcpy(buffer, recvData->data, recvData->length);
            memcpy(buffer + recvData->length, msg, len);
            free(recvData->data);
            recvData->data = NULL;
            recvData->data = buffer;
            recvData->length = length;
        }
        else
        {
            recvData->length = len;
            recvData->data = charCopy(msg, len);
        }
    }
    else
    {
        MNode *curNode = map->head;
        MNode *lastNode = NULL;
        while (curNode)
        {
            if (curNode->next == NULL)
            {
                lastNode = curNode;
                break;
            }
            curNode = curNode->next;
        }
        if (lastNode == NULL)
        {
            RecvData *recvData = malloc(sizeof(RecvData));
            recvData->length = len;
            recvData->data = NULL;
            recvData->data = charCopy(msg, len);
            MNode *appendNode = (MNode *)malloc(sizeof(MNode));
            appendNode->next = NULL;
            appendNode->record = (Record *)malloc(sizeof(Record));
            appendNode->record->sockfd = sockfd;
            appendNode->record->recvData = recvData;
            map->head = appendNode;
        }
        else
        {
            RecvData *recvData = (RecvData *)malloc(sizeof(RecvData));
            recvData->length = len;
            recvData->data = charCopy(msg, len);
            MNode *appendNode = (MNode *)malloc(sizeof(MNode));
            appendNode->next = NULL;
            appendNode->record = (Record *)malloc(sizeof(Record));
            appendNode->record->sockfd = sockfd;
            appendNode->record->recvData = recvData;
            lastNode->next = appendNode;
        }
    }
}

MNode *getbykey(Map *map, int id)
{
    if (isMapNull(map) == 1)
    {
        return NULL;
    }
    MNode *curNode = map->head;
    while (curNode)
    {
        if (id == curNode->record->sockfd)
        {
            return curNode;
        }
        curNode = curNode->next;
    }
    return NULL;
}
void removebykey(Map *map, int id)
{
    if (isMapNull(map))
    {
        return;
    }
    MNode *curNode = map->head;
    while (curNode)
    {
        if (curNode->next != NULL)
        {
            if (id == curNode->next->record->sockfd)
            {
                MNode *fnode = curNode->next;
                if (curNode->next->next != NULL)
                {
                    curNode->next = curNode->next->next;
                }
                else
                {
                    curNode->next = NULL;
                }
                free(fnode);
                fnode = NULL;
                break;
            }
        }
        else
        {
            MNode *fnode = curNode;
            if (id == curNode->record->sockfd)
            {
                map->head = curNode->next;
                free(fnode);
                fnode = NULL;
                break;
            }
        }
        curNode = curNode->next;
    }
}
void printMap(Map *map)
{
    if (isMapNull(map))
    {
        return;
    }
    MNode *curNode = map->head;
    while (curNode)
    {
        printf("id:%d  length:%d,data:%s\n,", curNode->record->sockfd, curNode->record->recvData->length, curNode->record->recvData->data);
        curNode = curNode->next;
    }
}

//1 means Null
int isMapNull(Map *map)
{
    if (map != NULL)
    {
        if (map->head == NULL)
        {
            return 1;
        }
        if (map->head != NULL)
        {
            return 0;
        }
    }
    return 1;
}